using System;
using System.Linq;
using System.Collections.Generic;
using Nop.Core.Domain.Discounts;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Security;
using Nop.Core.Domain.Seo;
using Nop.Core.Domain.Catalog;
using Nop.Core.ApiDataModel.Catalog;
using Nop.Core.Domain.Customers;

namespace Nop.Core.ApiDataModel
{
    /// <summary>
    /// Represents a Product MVC API DataModel.
    /// </summary>
    public partial class Product
    {
        /// <summary>
        /// Gets or sets the product identifier
        /// </summary>
        public virtual int ProductId { get; set; }

        /// <summary>
        /// Gets or sets the name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the description
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the old price
        /// </summary>
        public decimal OldPrice { get; set; }

        /// <summary>
        /// Gets or sets the price.
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// Member Price
        /// </summary>
        public decimal MemberPrice { get; set; }

        /// <summary>
        /// Available stock
        /// </summary>
        public int AvailableStock { get; set; }

        /// <summary>
        /// Store Name
        /// </summary>
        public string StoreName { get; set; }

        /// <summary>
        /// Gets or sets the collection of product picture IDs.
        /// </summary>
        public ICollection<string> ProductPictures { get; set; }

        public List<ProductVariantAttributeModel> Attributes { get; set; }


        //public ProductVariant ProductVariant { set; get; }

        /// <summary>
        /// Gets or sets a value indicating whether to show the product on home page
        /// </summary>
        public bool ShowOnHomePage { get; set; }
        /// <summary>
        /// Whether to display the Add button
        /// </summary>
        public bool IsShowButton { get; set; }

        public Product(Nop.Core.Domain.Catalog.Product product, Nop.Core.Domain.Catalog.ProductVariant productVariant, List<string> pictureUrls, bool isClubMember = false)
        {
            this.ProductId = productVariant.Id;
            this.Name = product.Name;
            this.Description = productVariant.Description;
            this.OldPrice = productVariant.OldPrice;
            this.Price = productVariant.Price;
            this.AvailableStock = productVariant.StockQuantity - productVariant.MinStockQuantity;
            this.StoreName = product.Store.Name;
            this.ShowOnHomePage = product.ShowOnHomePage;
            this.ProductPictures = new List<string>();
            pictureUrls.ForEach(url => this.ProductPictures.Add(url));


            this.Attributes = new List<ProductVariantAttributeModel>();
            if (productVariant.ProductVariantAttributes != null)
            {
                foreach (var attr in productVariant.ProductVariantAttributes)
                {
                    Attributes.Add(new ProductVariantAttributeModel(attr));
                }
            }

            if (productVariant.HasTierPrices && productVariant.TierPrices.Any())
            {
                var validTierPrices = productVariant.TierPrices.Where(tp => tp.Quantity == 1 &&
                    tp.CustomerRole.SystemName.Equals(SystemCustomerRoleNames.DreamFactoryUser)).ToList();
				if (validTierPrices.Any())
				{
					var minPrice = validTierPrices.Min(tp => tp.Price);
					this.MemberPrice = minPrice;
					//validTierPrices = validTierPrices.FilterForCustomer();
					if (isClubMember && validTierPrices.Any())
					{
						this.OldPrice = this.Price;
						this.Price = minPrice;
					}
				}
            }

        }
    }
}
